package com.atguigu.szdz.dcsyl.controller;

import com.alibaba.excel.EasyExcel;
import com.atguigu.szdz.dcsyl.common.utils.R;
import com.atguigu.szdz.dcsyl.entity.YlDataDc;
import com.atguigu.szdz.dcsyl.entity.YlDataEntity;
import com.atguigu.szdz.dcsyl.service.impl.WellInfoServiceImpl;
import com.atguigu.szdz.dcsyl.service.impl.YlDataServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.Pictures;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/export")
public class exportFile {

    @Autowired
    private YlDataServiceImpl ylDataServiceImpl;

    @Autowired
    private WellInfoServiceImpl wellInfoServiceImpl;

    @Operation(summary = "导出excel表格Eaton法计算的数据")
    @PostMapping("/exportFileEaton")
    public void exportFileEaton(HttpServletResponse response, @RequestParam("well_id") Integer wellId) throws IOException {
        // 1. 准备数据
        List<YlDataDc> ylDataDcList = new ArrayList<>();
        ylDataDcList = ylDataServiceImpl.getAllYlDataDc(wellId);
        // 2. 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 防止中文乱码
        String fileName = URLEncoder.encode("压力数据表.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        // 3. 执行导出
        EasyExcel.write(response.getOutputStream(), YlDataDc.class)
                .sheet("压力数据表")
                .doWrite(ylDataDcList);
    }

    @Operation(summary = "导出excel表格Bowers法计算的数据")
    @PostMapping("/exportFileBowers")
    public void exportFileBowers(HttpServletResponse response, @RequestParam("well_id") Integer wellId) throws IOException {
        // 1. 准备数据
        List<YlDataEntity> ylDataDcList = new ArrayList<>();
        ylDataDcList = ylDataServiceImpl.getAllYlData();
        // 2. 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 防止中文乱码
        String fileName = URLEncoder.encode("压力数据表.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        // 3. 执行导出
        EasyExcel.write(response.getOutputStream(), YlDataEntity.class)
                .sheet("压力数据表")
                .doWrite(ylDataDcList);
    }

    @Operation(summary = "导出docx文档Eaton法计算的数据")
    @PostMapping("/exportReportEaton")
    public void exportReportEaton(HttpServletResponse response,@RequestParam("image") MultipartFile image,@RequestParam("well_id") Integer wellId) throws IOException {
        //1.读模板
        InputStream in = new ClassPathResource("doc/PreVue地层压力监测总结报告模板.docx").getInputStream();
        XWPFTemplate template = XWPFTemplate.compile(in);
        // 2. 准备数据
        Map<String, Object> wellInfo = wellInfoServiceImpl.getWellInfoDetail(wellId);
        //获得井名
        String wellName = (String) wellInfo.get("well_name");
        //获得坐标x
        Double locx = (Double) wellInfo.get("locx");
        //获得坐标y
        Double locy = (Double) wellInfo.get("locy");
        //获得补心海拔
        Double kb = (Double) wellInfo.get("kb");
        //获得水深
        Double wd = (Double) wellInfo.get("wd");
        Map<String,Object> data = new HashMap<>();
        data.put("wellName",wellName);
        data.put("locx",locx);
        data.put("locy",locy);
        data.put("kb",kb);
        data.put("wd",wd);
        data.put("date", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月")));
        //图片数据
        PictureRenderData img = Pictures.ofStream(image.getInputStream()).center().size(580,650).create();
        data.put("img",img);
        //渲染
        template.render(data);
        template.writeAndClose(response.getOutputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition",
                "attachment; filename*=UTF-8''" + URLEncoder.encode("井报告_" + wellName + ".docx", "UTF-8"));
    }
}
